package com.nbpt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    GridView gridView;
    String name[] = {"智能家居","智能商超","天气预报","设置中心"};
    int image[]={R.drawable.icon_home,R.drawable.icon_shopping,R.drawable.icon_weather,R.drawable.icon_settings,};
    String from[] ={"name","image"};
    int to[] = {R.id.textView3,R.id.imageview};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

      gridView = findViewById(R.id.gridview);
      List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
      for (int i=0; i<name.length;i++) {
          Map<String, Object> map = new HashMap<String, Object>();
          map.put("name", name[i]);
          map.put("image", image[i]);
          data.add(map);
      }

        SimpleAdapter adapter = new SimpleAdapter(this,data,
                R.layout.gridview_item,from,to);
        gridView.setAdapter(adapter);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(this, "点击了"+name[i], Toast.LENGTH_LONG).show();
        if(i==0) {
            Intent intent = new Intent(MenuActivity.this, HomeActivity.class);
            startActivityForResult(intent,0);
        }
        if (i==1){
            Intent intent = new Intent(MenuActivity.this, ShopActivity.class);
            startActivityForResult(intent,0);
        }
        if(i==2){
            Intent intent = new Intent(MenuActivity.this, WeatherActivity.class);
            startActivityForResult(intent,0);
        }
        if(i==3){
            Intent intent = new Intent(MenuActivity.this, SettingActivity.class);
            startActivityForResult(intent,0);
        }
    }
}
